﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;

namespace LGSVR.Common
{
    public static class Utils
    {
        /// <summary>
        /// 验证数字
        /// </summary>
        /// <param name="vStr">验证字符串</param>
        /// <returns></returns>
        public static bool IsNumber(string vStr)
        {
            bool isTrue;

            if (!string.IsNullOrEmpty(vStr))
            {
                vStr = vStr.Trim();
                isTrue = Regex.IsMatch(vStr, @"^\d+$");
            }
            else
            {
                isTrue = false;
            }

            return isTrue;
        }

        public static bool ToBool(string vStr)
        {
            bool isTrue;

            if (!string.IsNullOrEmpty(vStr))
            {
                vStr = vStr.Trim();

                if (vStr == "1" || vStr == "0")
                {
                    if (vStr == "1")
                    {
                        isTrue = true;
                    }
                    else
                    {
                        isTrue = false;
                    }
                }
                else
                {
                    isTrue = bool.Parse(vStr);
                }
            }
            else
            {
                isTrue = false;
            }

            return isTrue;
        }

        public static DataTable EnumToDataTable(Type enumType)
        {
            string[] Names = System.Enum.GetNames(enumType);

            Array Values = System.Enum.GetValues(enumType);

            DataTable table = new DataTable();
            table.Columns.Add("ID", System.Type.GetType("System.Int32"));
            table.Columns.Add("Name", System.Type.GetType("System.String"));
            table.Columns["ID"].Unique = true;
            for (int i = 0; i < Values.Length; i++)
            {
                DataRow DR = table.NewRow();
                DR["ID"] = (int)Values.GetValue(i);
                DR["Name"] = Names[i].ToString();
                table.Rows.Add(DR);
            }
            return table;
        }

    }
}
